//
//  TTChangeHandSizePopView.m
//  TaoTuo
//
//  Created by admin on 2019/12/18.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTChangeHandSizePopView.h"
#import "TTChangeHandSizeViewCell.h"

static NSString *cellID = @"cell";

@implementation HandSizeModel

+ (instancetype)setModelWithHandSize:(int)handSize
{
    HandSizeModel *model = [[self alloc] init];
    model.handSize = handSize;
    return model;
}

@end

@interface TTChangeHandSizePopView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) int currentHandSize;
@property (nonatomic, copy) SelectBlock selectBlock;
@property (nonatomic, assign) int selectSize;   //选中的手寸

///上次选中的那个
@property (nonatomic, strong) NSIndexPath *lastIndexPath;

@end

@implementation TTChangeHandSizePopView

+ (instancetype)changeHandSizeViewWithCurrentHandSize:(int)currentHandSize selectBlock:(nonnull SelectBlock)selectBlock
{
    TTChangeHandSizePopView *view = [[TTChangeHandSizePopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 60, 360)];
    view.backgroundColor = [UIColor whiteColor];
    view.currentHandSize = currentHandSize;
    view.selectBlock = selectBlock;
    [view setupSubviews];
    [view setupDatas];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTChangeHandSizeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HandSizeModel *model = self.dataArray[indexPath.row];
    if (model.isCurrent) {
        return;
    }
    
    HandSizeModel *lastModel = self.dataArray[self.lastIndexPath.row];
    lastModel.isSelect = NO;
    
    model.isSelect = YES;
    
    [self.tableView reloadData];
    
    self.selectSize = model.handSize;
    self.lastIndexPath = indexPath;
}

- (void)setupDatas
{
    int min = self.currentHandSize - 2 < 0 ? 0 : self.currentHandSize - 2;
    int max = self.currentHandSize + 2;
    
    for (int i = max; i >= min; i--) {
        HandSizeModel *model = [HandSizeModel setModelWithHandSize:i];
        if (i == self.currentHandSize) {
            model.isCurrent = YES;
        }
        if (i == max) {
            model.isSelect = YES;
        }
        [self.dataArray addObject:model];
    }
    
    self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    self.selectSize = max;
    [self.tableView reloadData];
}

///取消
- (void)cancelAction
{
    [self hideInWindow];
}

///确定
- (void)confirmAction
{
    if (self.selectBlock) {
        self.selectBlock(self.selectSize);
    }
    
    [self hideInWindow];
    
}

- (void)setupSubviews
{
    self.layer.cornerRadius = 10;
    UILabel *titleLabel = [UILabel labelTextCenterWithTitle:@"改手寸" color:[UIColor blackColor] fontSize:15];
    titleLabel.frame = CGRectMake(0, 0, self.width, 50);
    [self addSubview:titleLabel];
    
    self.tableView.frame = CGRectMake(0, titleLabel.bottom, self.width, 250);
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(0, self.tableView.bottom + 10) endPoint:CGPointMake(self.width, self.tableView.bottom + 10) lineColor:kSeparatorColor lineWidth:0.5];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(self.width * 0.5, self.tableView.bottom + 10) endPoint:CGPointMake(self.width * 0.5, self.bottom) lineColor:kSeparatorColor lineWidth:0.5];
    
    UIButton *cancleBtn = [UIButton createButtonWithTitle:@"取消" titleColor:UIColorHex(5A5A5A) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(cancelAction)];
    cancleBtn.frame = CGRectMake(0, self.tableView.bottom + 10, self.width * 0.5, 50);
    [self addSubview:cancleBtn];
    
    UIButton *confirmBtn = [UIButton createButtonWithTitle:@"确定" titleColor:kMainColor titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(confirmAction)];
    confirmBtn.frame = CGRectMake(cancleBtn.right, cancleBtn.top, self.width * 0.5, 50);
    [self addSubview:confirmBtn];
    
    
}



- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 50;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTChangeHandSizeViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [self addSubview:_tableView];
        
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
